package cn.chia.front.action;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;

import cn.chia.pojo.Iteminfoview;
import cn.chia.pojo.TblActivityType;
import cn.chia.pojo.TblCatagorgy;
import cn.chia.pojo.TblItems;
import cn.chia.pojo.User;
import cn.chia.service.ICatagorgyService;
import cn.chia.service.IItemService;
import cn.chia.service.IShopcarService;

import com.opensymphony.xwork2.ActionSupport;

@Controller
public class ItemAction extends ActionSupport{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Logger logger = Logger.getLogger(ItemAction.class);
	private IItemService itemServiceImpl;
	private ICatagorgyService catagorgyServiceImpl;
	private IShopcarService iShopcarService;
	private List<TblCatagorgy> catagorgyFristList; 
	private List<TblCatagorgy> catagorgySecondList; 	
	private List<TblItems> findByActivityList;
	
	private List<Iteminfoview> findByCatagorgyList;
	private List<Iteminfoview> findByDiscountList;
	private List<Iteminfoview> findByFullList;
	private List<Iteminfoview> findByNameList;
	private List<Iteminfoview> findByFirstNameList;
	private List<Iteminfoview> allList;
	private List<TblActivityType> findActivityTypeList;
	
	
	private TblCatagorgy tblCatagorgy;
	private TblItems tblItems;
	private TblActivityType tblActivityType;
	
	private String itemId;
	private String keyWords;
	
	
	/**
	 * @author 徐艳慧
	 * @Todo 查询全部一级分类, 查询活动的全部种类.
	 * @throws Exception 
	 */
	public String first() throws Exception{
		catagorgyFristList = catagorgyServiceImpl.getByFirst();
		findActivityTypeList = itemServiceImpl.findActivityType();
		User user = (User) ServletActionContext.getRequest().getSession().getAttribute("user");
		ServletActionContext.getRequest().setAttribute("count", iShopcarService.findAll(user).size());
		return "first";
	}
	
	/**
	 * @author 徐艳慧
	 * @Todo 查询某个一级分类对应的二级分类
	 */
	public String second() throws Exception{
		catagorgySecondList = catagorgyServiceImpl.getBySecond(tblCatagorgy.getCatagorgyId());
		User user = (User) ServletActionContext.getRequest().getSession().getAttribute("user");
		ServletActionContext.getRequest().setAttribute("count", iShopcarService.findAll(user).size());
		return "second";
	}
	
	
	

	/**
	 * @author 徐艳慧
	 * @Todo 按照种类名称来查询商品。
	 */
	public String findByCatagorgy() throws Exception{
		logger.info("++++++++++++++++++++++"+tblCatagorgy.getCatagorgyName()+"=======================++++++++++++++");
		String buf = new String(tblCatagorgy.getCatagorgyName().getBytes("ISO-8859-1") ,"utf-8");
		findByCatagorgyList = itemServiceImpl.findByCatagorgy(buf);
		buf=null;
		User user = (User) ServletActionContext.getRequest().getSession().getAttribute("user");
		ServletActionContext.getRequest().setAttribute("count", iShopcarService.findAll(user).size());
		return "findByCatagorgy";
	}


	/**
	 * @author 徐艳慧
	 * @Todo 查询全部单品打折的商品
	 */
	public String findByDiscount()throws Exception{
		findByDiscountList = itemServiceImpl.findByActivityType("单品打折");
		User user = (User) ServletActionContext.getRequest().getSession().getAttribute("user");
		ServletActionContext.getRequest().setAttribute("count", iShopcarService.findAll(user).size());
		return "findByDiscount";
	}
	
	/**
	 * @author 徐艳慧
	 * @Todo 查询全部满减的商品
	 */
	public String findByFull()throws Exception{
		findByFullList = itemServiceImpl.findByActivityType("满减活动");
		User user = (User) ServletActionContext.getRequest().getSession().getAttribute("user");
		ServletActionContext.getRequest().setAttribute("count", iShopcarService.findAll(user).size());
		return "findByFull";
	}
	
	/**
	 * @author 徐艳慧
	 * @Todo 查询全部商品
	 */
	public String findAll()throws Exception{
		allList = itemServiceImpl.findAll();
		User user = (User) ServletActionContext.getRequest().getSession().getAttribute("user");
		ServletActionContext.getRequest().setAttribute("count", iShopcarService.findAll(user).size());
		return "findAll";
	}
	
	
	/**
	 * @author 徐艳慧
	 * @return 按照商品名称查商品
	 * @throws Exception
	 */
	public String findByName()throws Exception{
		
		findByNameList = itemServiceImpl.findByName(tblItems.getItemName());
	
		User user = (User) ServletActionContext.getRequest().getSession().getAttribute("user");
		ServletActionContext.getRequest().setAttribute("count", iShopcarService.findAll(user).size());
		return "findByName";
	}
	
	
	public String findByFirstName() throws Exception{
		findByFirstNameList = itemServiceImpl.findByFirstName(tblCatagorgy.getCatagorgyId());
		//图片处理
		if(findByFirstNameList.size()>0){
			for(int i=0; i<findByFirstNameList.size(); i++){
				String [] str = findByFirstNameList.get(i).getItemPic().split(",");
				if(str.length>1){
					findByFirstNameList.get(i).setItemPic("http://market.chi-a.cn/commoditymanagement/resources/upload/"+str[1]);
				}
			}
		}
		return "findByFirstName";
	}
	
	
	
	public String findByKeyWords()throws Exception{
		findByNameList = itemServiceImpl.findByName(keyWords);
		return "findByKeyWords";
	}


	public List<TblCatagorgy> getCatagorgyFristList() {
		return catagorgyFristList;
	}

	public void setCatagorgyFristList(List<TblCatagorgy> catagorgyFristList) {
		this.catagorgyFristList = catagorgyFristList;
	}

	
	
	public List<Iteminfoview> getFindByNameList() {
		return findByNameList;
	}

	public void setFindByNameList(List<Iteminfoview> findByNameList) {
		this.findByNameList = findByNameList;
	}

	public List<TblCatagorgy> getCatagorgySecondList() {
		return catagorgySecondList;
	}

	public void setCatagorgySecondList(List<TblCatagorgy> catagorgySecondList) {
		this.catagorgySecondList = catagorgySecondList;
	}



	public TblCatagorgy getTblCatagorgy() {
		return tblCatagorgy;
	}

	public void setTblCatagorgy(TblCatagorgy tblCatagorgy) {
		this.tblCatagorgy = tblCatagorgy;
	}

	

	public List<Iteminfoview> getFindByCatagorgyList() {
		return findByCatagorgyList;
	}

	public void setFindByCatagorgyList(List<Iteminfoview> findByCatagorgyList) {
		this.findByCatagorgyList = findByCatagorgyList;
	}

	public TblItems getTblItems() {
		return tblItems;
	}

	public void setTblItems(TblItems tblItems) {
		this.tblItems = tblItems;
	}

	
	public String getItemId() {
		return itemId;
	}

	public void setItemId(String itemId) {
		this.itemId = itemId;
	}

	public List<TblActivityType> getFindActivityTypeList() {
		return findActivityTypeList;
	}

	public void setFindActivityTypeList(List<TblActivityType> findActivityTypeList) {
		this.findActivityTypeList = findActivityTypeList;
	}

	public List<TblItems> getFindByActivityList() {
		return findByActivityList;
	}

	public void setFindByActivityList(List<TblItems> findByActivityList) {
		this.findByActivityList = findByActivityList;
	}

	
	public TblActivityType getTblActivityType() {
		return tblActivityType;
	}

	

	public void setTblActivityType(TblActivityType tblActivityType) {
		this.tblActivityType = tblActivityType;
	}

	public String getKeyWords() {
		return keyWords;
	}

	public void setKeyWords(String keyWords) {
		this.keyWords = keyWords;
	}

	
	

	public List<Iteminfoview> getAllList() {
		return allList;
	}

	public void setAllList(List<Iteminfoview> allList) {
		this.allList = allList;
	}

	public List<Iteminfoview> getFindByDiscountList() {
		return findByDiscountList;
	}

	public void setFindByDiscountList(List<Iteminfoview> findByDiscountList) {
		this.findByDiscountList = findByDiscountList;
	}

	
	
	public List<Iteminfoview> getFindByFirstNameList() {
		return findByFirstNameList;
	}

	public void setFindByFirstNameList(List<Iteminfoview> findByFirstNameList) {
		this.findByFirstNameList = findByFirstNameList;
	}

	public List<Iteminfoview> getFindByFullList() {
		return findByFullList;
	}

	public void setFindByFullList(List<Iteminfoview> findByFullList) {
		this.findByFullList = findByFullList;
	}

	@Resource(name="catagorgyServiceImpl")
	public void setCatagorgyServiceImpl(ICatagorgyService catagorgyServiceImpl) {
		this.catagorgyServiceImpl = catagorgyServiceImpl;
	}

	@Resource(name="shopcarServiceImpl")
	public void setiShopcarService(IShopcarService iShopcarService) {
		this.iShopcarService = iShopcarService;
	}
	@Resource(name="itemServiceImpl")
	public void setItemServiceImpl(IItemService itemServiceImpl) {
		this.itemServiceImpl = itemServiceImpl;
	}
	
}
